//
//  BDJTabBarController.m
//  BuDeJie
//
//  Created by Guo Rui on 2017/9/19.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "BDJTabBarController.h"
#import "BDJEssenceViewController.h"
#import "BDJFriendTrendsViewController.h"
#import "BDJNewViewController.h"
#import "BDJMeViewController.h"
#import "BDJTabBar.h"
#import "BDJNavigationViewController.h"

static NSString * const kTabBarChildViewControllerTitleKey = @"com.timeface.tabbarcontroller.childviewcontrollertitlekey";
static NSString * const kTabBarChildViewControllerSelImageNameKey = @"com.timeface.tabbarcontroller.childviewcontrollersleimagenamekey";
static NSString * const kTabBarChildViewControllerNorImageNameKey = @"com.timeface.tabbarcontroller.childviewcontrollernorimagenamekey";

@interface BDJTabBarController ()<UITabBarControllerDelegate>
//@property (weak, nonatomic) UIViewController *previousSelectVc;
@end

@implementation BDJTabBarController

+ (void)load
{
    // 1. appearanceWhenContainedInInstancesOfClasses 可以控制apperance只在当前类中生效。
    UITabBarItem *apperance = [UITabBarItem appearanceWhenContainedInInstancesOfClasses:@[self]];
    [apperance setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor] } forState:UIControlStateSelected];
    
    // 2. 设置字体大小，只能在UIControlStateNormal状态设置才有效。
    [apperance setTitleTextAttributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:13] } forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupAllChildViewControllers];
    
//    self.previousSelectVc = self.childViewControllers.firstObject;
    
    BDJTabBar *tabBar = [[BDJTabBar alloc] init];
    tabBar.frame = self.tabBar.bounds;
    [self setValue:tabBar forKey:@"tabBar"];
    
    self.delegate = self;
}

//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
//{
//    if (self.previousSelectVc == viewController) {
//        BDJFUNC
//    }
//    self.previousSelectVc = viewController;
//}

- (void)setupAllChildViewControllers
{
    for (NSDictionary *info in [self viewControllerInfos]) {
        NSString *key = info.allKeys.firstObject;
        NSDictionary *value = info[key];
        Class class = NSClassFromString(key);
        UIViewController *viewController = [[class alloc] init];
        BDJNavigationViewController *nvc = [[BDJNavigationViewController alloc] initWithRootViewController:viewController];
        [self addChildViewController:nvc];
        
        // 配置tabbar
        nvc.tabBarItem.title = value[kTabBarChildViewControllerTitleKey];
        nvc.tabBarItem.image = [UIImage imageNamed:value[kTabBarChildViewControllerNorImageNameKey]];
        nvc.tabBarItem.selectedImage = [[UIImage imageNamed:value[kTabBarChildViewControllerSelImageNameKey]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
}

- (NSArray *)viewControllerInfos
{
    return @[ @{NSStringFromClass([BDJEssenceViewController class]) : @{ kTabBarChildViewControllerTitleKey : @"精华", kTabBarChildViewControllerNorImageNameKey : @"tabBar_essence_icon", kTabBarChildViewControllerSelImageNameKey : @"tabBar_essence_click_icon" } },
              
              @{NSStringFromClass([BDJNewViewController class]) : @{ kTabBarChildViewControllerTitleKey : @"新帖", kTabBarChildViewControllerNorImageNameKey : @"tabBar_new_icon", kTabBarChildViewControllerSelImageNameKey : @"tabBar_new_click_icon" } },
              
              @{NSStringFromClass([BDJFriendTrendsViewController class]) : @{ kTabBarChildViewControllerTitleKey : @"关注", kTabBarChildViewControllerNorImageNameKey : @"tabBar_friendTrends_icon", kTabBarChildViewControllerSelImageNameKey : @"tabBar_friendTrends_click_icon" } },
              
              @{NSStringFromClass([BDJMeViewController class]) : @{ kTabBarChildViewControllerTitleKey : @"我", kTabBarChildViewControllerNorImageNameKey : @"tabBar_me_icon", kTabBarChildViewControllerSelImageNameKey : @"tabBar_me_click_icon" } }];
}

@end
